package com.zyy.smartcity1129.violation;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.ViolationRecordInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: ViolationRecordFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 7/12/2021$
 * @Version: 1.0
 */
public class ViolationRecordFragment extends BaseFragment {
    private String carNum = ViolationFragment.carNum;
    private RecyclerView rvOnly;
    private Button btnNext;
    private TextView tvOnly;
    RvAd<ViolationRecordInfo.RowsBean> rvAd;
    public static ViolationRecordInfo.RowsBean violation;
    List<ViolationRecordInfo.RowsBean> records;
    List<ViolationRecordInfo.RowsBean> record;
    private Button btnMore;

    @Override
    protected int layId() {
        return R.layout.f_rv_only;
    }

    @Override
    protected String title() {
        return "违章记录";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        rvOnly.setLayoutManager(new LinearLayoutManager(getContext()));
        rvOnly.setVisibility(View.GONE);
        tvOnly.setVisibility(View.VISIBLE);
        th(() -> {
            List<ViolationRecordInfo.RowsBean> recordList = Http.getInstance().get("/prod-api/api/traffic/illegal/list", null, ViolationRecordInfo.class, 1).getRows();
            ui(() -> {
                for (ViolationRecordInfo.RowsBean r : recordList) {
                    if (r.getLicencePlate() != null) {
                        if (r.getLicencePlate().equals(carNum)) {
                            tvOnly.setVisibility(View.GONE);
                            rvOnly.setVisibility(View.VISIBLE);
                            record = recordList.stream().filter(a -> a.getLicencePlate().equals(carNum)).collect(Collectors.toList());
                            if (record.size() > 5) {
                                records = record.subList(0, 5);
                                btnMore.setVisibility(View.VISIBLE);
                            } else {
                                records = record;
                            }
                            initAdapter(records);
                            rvOnly.setAdapter(rvAd);
                            break;
                        }
                    }
                }
                btnMore.setOnClickListener(view -> {
                    initAdapter(record);
                    rvOnly.setAdapter(rvAd);
                });
            });
        });
    }

    private void initAdapter(List<ViolationRecordInfo.RowsBean> recordList) {
        rvAd = new RvAd<ViolationRecordInfo.RowsBean>(recordList, R.layout.rv_base) {
            private void initView(View vi) {
                ig = (ImageView) vi.findViewById(R.id.ig);
                view = (View) vi.findViewById(R.id.view);
                lineRvPatient = (LinearLayout) vi.findViewById(R.id.lineRvPatient);
                tv1 = (TextView) vi.findViewById(R.id.tv1);
                tv2 = (TextView) vi.findViewById(R.id.tv2);
                tv3 = (TextView) vi.findViewById(R.id.tv3);
                tv4 = (TextView) vi.findViewById(R.id.tv4);
                tv5 = (TextView) vi.findViewById(R.id.tv5);
                tv6 = (TextView) vi.findViewById(R.id.tv6);
                tv7 = (TextView) vi.findViewById(R.id.tv7);
                imgRvPatient = (ImageView) vi.findViewById(R.id.imgRvPatient);
                imgDowm = (ImageView) vi.findViewById(R.id.imgDowm);
                rv = (RecyclerView) vi.findViewById(R.id.rv);
                ig.setVisibility(View.GONE);
                view.setVisibility(View.GONE);
            }

            private RecyclerView rv;
            private ImageView imgDowm;
            private ImageView imgRvPatient;
            private TextView tv7;
            private TextView tv6;
            private TextView tv5;
            private TextView tv4;
            private TextView tv3;
            private TextView tv2;
            private TextView tv1;
            private LinearLayout lineRvPatient;
            private View view;
            private ImageView ig;

            @Override
            protected void initR(View vi, ViolationRecordInfo.RowsBean rowsBean, int p) {
                initView(vi);
                tv1.setText("违法时间：" + rowsBean.getBadTime());
                tv2.setText("违章地点：" + rowsBean.getIllegalSites());
                tv3.setText("违章记分：" + rowsBean.getDeductMarks() + "分");
                tv4.setText("罚款金额：" + rowsBean.getMoney() + "元");
                tv5.setText("处理状态:" + rowsBean.getDisposeState());
                vi.setOnClickListener(view1 -> {
                    violation = rowsBean;
                    add(new ViolationDetailedFragment());
                });
            }
        };
    }

    private void initView(View iv) {
        rvOnly = (RecyclerView) iv.findViewById(R.id.rvOnly);
        btnNext = (Button) iv.findViewById(R.id.btnNext);
        tvOnly = (TextView) iv.findViewById(R.id.tvOnly);
        btnMore = (Button) iv.findViewById(R.id.btnMore);
    }
}
